package com.lmn.spel;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class Spel implements ApplicationRunner {

    public static final Long DEFAULT_V = 10L;

    //引用Bean、属性和方法（必须是public修饰的）
    @Value("#{5}")
    private Long v1;

    //算术运算符：+,-,*,/,%,^
    @Value("#{5+2}")
    private Long v2;

    //字符串连接符：+
    @Value("#{'a' + 'b'}")
    private String v3;

    //and,or,not,&&(&&),||,!
    //比较运算符：<(<),>(>),==,<=,>=,lt,gt,eq,le,ge
    @Value("#{(1 > 2) or (5 > 8)}")
    private Boolean v4;

    //条件运算符：?true:false
    @Value("#{(10>3)? '真' : '假'}")
    private String v5;

    //正则表达式：matches
    //@Value("#{DEFAULT_V matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}'}")
    private Boolean v6;

    /*调用静态方法或静态属性,通过 T() 调用一个类的静态方法，它将返回一个 Class Object，然后再调用相应的方法或属性：*/
    @Value("#{T(java.lang.Math).PI}")
    private Float v7;
    /**
     * .^[]：查询符合条件的第一个元素
     * .$[]：查询符合条件的最后一个元素
     */
    //@Value("#{cities.![name]}")
    private Long v8;

    //@Value("#{cities.![name+','+state]}")
    private Long v9;

    //@Value("#{cities.?[population gt 10].![name+','+state]}")
    private Long v10;

    @Value("${user.name}")
    private String name;

    @Value("${user.tools}")
    private String[] tools;

    @Value("${user.tools}")
    private String[] tools1;

    @Value("#{systemProperties['os.name']}")
    private String systemPropertiesName;

    /**
     * 注入列表形式（自动根据"|"分割）
     */
    @Value("#{'${user.words}'.split('\\|')}")
    private List<String> numList;

    @Value("#{systemProperties['port']?:'8888'}")
    private String port;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("SPEL:::" + this.toString());

    }

    @Override
    public String toString() {
        return "Spel{" +
                "v1=" + v1 +
                ", v2=" + v2 +
                ", v3='" + v3 + '\'' +
                ", v4=" + v4 +
                ", v5='" + v5 + '\'' +
                ", v6=" + v6 +
                ", v7=" + v7 +
                ", v8=" + v8 +
                ", v9=" + v9 +
                ", v10=" + v10 +
                ", name='" + name + '\'' +
                ", tools=" + Arrays.toString(tools) +
                ", tools1=" + Arrays.toString(tools1) +
                ", systemPropertiesName='" + systemPropertiesName + '\'' +
                ", numList=" + numList +
                ", port='" + port + '\'' +
                '}';
    }
}
